/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Collections;
using ELE.Common.Util;

namespace ELE.EnterpriseLibrary.Naming
{
  /// <summary>
  /// Summary description for RemotingContextMonitor.
  /// 
  /// Author : Miguel Curi
  /// 
  /// </summary>
  public class RemotingContextMonitor : MarshalByRefObject
  {
    /// <summary>
    /// 
    /// </summary>
    public RemotingContextMonitor(){}
    /// <summary>
    /// Returns a table of available remote objects keyed by name served by the Static Context
    /// </summary>
    /// <returns></returns>
    public Hashtable GetListOfRemoteObjects()
    {
      return GetListOfRemoteObjects(null);
    }
    /// <summary>
    /// Returns a table of available remote objects keyed by name served by the 
    /// sub context identified by the given id
    /// </summary>
    /// <param name="contextId">id of the context (initial for the default initital context)</param>
    /// <returns></returns>
    public Hashtable GetListOfRemoteObjects(string contextId)
    {
      try
      {

        if(contextId == null )
        {
          return InitialContext.StaticContext.RegisteredRemoteServices;
        }
        else if (StringUtils.EqualsIgnoreCase(contextId, "initial"))
        {
          INamingContext defaultctx = InitialContext.GetContext();
          if(defaultctx == null)
          {
            return GetListOfRemoteObjects();
          }
          else if(defaultctx is InitialContext)
          {
            return ((InitialContext)defaultctx).RegisteredRemoteServices;
          }
          return null;
        }
        INamingContext ctx = InitialContext.GetContext(contextId);
        if(ctx != null && ctx is InitialContext)
        {
          return ((InitialContext)ctx).RegisteredRemoteServices;
        }
      }
      catch{}
      return null;

    }
  }
}
